package com.spremoteservice.android;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class WebsService {
	
	private String _siteUrl;
	private String _userName;
	private String _password;
	
	public WebsService(String siteUrl, String userName, String password){
		this._siteUrl = siteUrl;
		this._userName = userName;
		this._password = password;
	}
	public ListViewSPObject GetWeb() throws ClientProtocolException, IOException, ParserConfigurationException, SAXException{
		ListViewSPObject SPWeb = null;
		HttpClient httpclient = new DefaultHttpClient();        
        ((AbstractHttpClient) httpclient).getAuthSchemes().register("ntlm",new NTLMSchemeFactory());

        NTCredentials creds = new NTCredentials(_userName, _password, "", "");

        ((AbstractHttpClient) httpclient).getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
        HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 5000); 
        
        HttpPost httppost = new HttpPost(_siteUrl + "/_vti_bin/Webs.asmx");
        httppost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
	        
    	StringEntity se = new StringEntity("<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><GetWeb xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><webUrl>.</webUrl></GetWeb></s:Body></s:Envelope>", HTTP.UTF_8);
        se.setContentType("text/xml");
        httppost.setEntity(se); 

        HttpResponse httpresponse = httpclient.execute(httppost);
        HttpEntity resEntity = httpresponse.getEntity();

	    String GetWebResult = EntityUtils.toString(resEntity);
	   
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	        
    	DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(new StringReader(GetWebResult)));
        
        NodeList webNodeList = doc.getElementsByTagName("Web");
        Node webNode = webNodeList.item(0);
        NamedNodeMap atts = webNode.getAttributes();
        
        //Create objectr
        String title = atts.getNamedItem("Title").getNodeValue();
        String id = atts.getNamedItem("Id").getNodeValue();
        String objectType = "RootSite";
        
        SPWeb =  new ListViewSPObject(id, objectType, title, _siteUrl, "", "", "", "");
        
        return SPWeb;
	}
}
